import os
from pathlib import Path
import yaml

# 读取config.yml
def load_yaml_config():
    config_path = Path(__file__).parent / "config.yml"
    if config_path.exists():
        with open(config_path, "r", encoding="utf-8") as f:
            return yaml.safe_load(f)
    return {}

_yaml_config = load_yaml_config()

def get_config(key, default=None):
    # 优先环境变量，其次yml，最后默认
    return os.environ.get(key) or _yaml_config.get(key) or default

# 基础配置
BASE_DIR = Path(__file__).parent.absolute()
VIDEO_SOURCE_DIR = get_config("VIDEO_SOURCE_DIR", os.path.join(BASE_DIR, "/app/videos"))
PREVIEW_SAVE_DIR = get_config("PREVIEW_SAVE_DIR", os.path.join(BASE_DIR, "/app/previews"))
FAVORITE_DIR = get_config("FAVORITE_DIR", os.path.join(BASE_DIR, "/app/favorites"))
DATABASE_PATH = get_config("DATABASE_PATH", os.path.join(BASE_DIR, "/app/previews/preview.db"))

# FFmpeg配置
FFMPEG_PATH = get_config("FFMPEG_PATH", "ffmpeg")
FFPROBE_PATH = get_config("FFPROBE_PATH", "ffprobe")
MIN_VIDEO_DURATION = int(get_config("MIN_VIDEO_DURATION", 30))
MID_VIDEO_DURATION = int(get_config("MID_VIDEO_DURATION", 300))

# GIF配置
PREVIEW_WIDTH = int(get_config("PREVIEW_WIDTH", 320))
PREVIEW_HEIGHT = int(get_config("PREVIEW_HEIGHT", 240))
PREVIEW_DURATION = int(get_config("PREVIEW_DURATION", 3))
GIF_FPS = int(get_config("GIF_FPS", 10))
GIF_QUALITY = int(get_config("GIF_QUALITY", 80))

PER_PAGE = int(get_config("PER_PAGE", 12))
